#include "..\\stdafx.h"
#include "LatestServiceClient.h"
#include "..\\..\\facecat\\include\\core\\FCBinary.h"

LatestServiceClient::LatestServiceClient(){
    setServiceID(10003);
    m_connected = false;
    m_socketID = -1;
}

int LatestServiceClient::getSocketID() {
    return m_socketID;
}

void LatestServiceClient::setSocketID(int value) {
    m_socketID = value;
}

void LatestServiceClient::subCodes(int requestID, String codes) {
    FCBinary bw;
    bw.writeString(codes);
    char* bytes = (char*)bw.getBytes();
    FCMessage fcm(getServiceID(), FUNCTION_SUBCODES, requestID, m_socketID, 0, getCompressType(), bw.getBytesLen(), bytes);
    send(&fcm);
}

void LatestServiceClient::unSubCodes(int requestID) {
    FCBinary bw;
    bw.writeString("1");
    char* bytes = (char*)bw.getBytes();
    FCMessage fcm(getServiceID(),FUNCTION_UNSUBCODES, requestID, m_socketID, 0, getCompressType(), bw.getBytesLen(), bytes);
    send(&fcm);
}
SecurityLatestData LatestServiceClient::getLatestData(char* body, int bodyLength) {
    FCBinary br;
    br.write(body, bodyLength);
    SecurityLatestData latestData;
    latestData.m_code = br.readString();
    latestData.m_open = br.readFloat();
    latestData.m_lastClose = br.readFloat();
    latestData.m_close = br.readFloat();
    latestData.m_high = br.readFloat();
    latestData.m_low = br.readFloat();
    latestData.m_volume = br.readDouble();
    latestData.m_amount = br.readDouble();
    latestData.m_buyPrice1 = br.readFloat();
    latestData.m_buyVolume1 = br.readInt();
    latestData.m_buyPrice2 = br.readFloat();
    latestData.m_buyVolume2 = br.readInt();
    latestData.m_buyPrice3 = br.readFloat();
    latestData.m_buyVolume3 = br.readInt();
    latestData.m_buyPrice4 = br.readFloat();
    latestData.m_buyVolume4 = br.readInt();
    latestData.m_buyPrice5 = br.readFloat();
    latestData.m_buyVolume5 = br.readInt();
    latestData.m_sellPrice1 = br.readFloat();
    latestData.m_sellVolume1 = br.readInt();
    latestData.m_sellPrice2 = br.readFloat();
    latestData.m_sellVolume2 = br.readInt();
    latestData.m_sellPrice3 = br.readFloat();
    latestData.m_sellVolume3 = br.readInt();
    latestData.m_sellPrice4 = br.readFloat();
    latestData.m_sellVolume4 = br.readInt();
    latestData.m_sellPrice5 = br.readFloat();
    latestData.m_sellVolume5 = br.readInt();
    latestData.m_turnoverRate = br.readFloat();
    latestData.m_pe = br.readFloat();
    latestData.m_marketValue = br.readFloat();
    latestData.m_flow = br.readFloat();
    latestData.m_ratio = br.readFloat();
    latestData.m_equity = br.readDouble();
    latestData.m_flowEquity = br.readDouble();
    latestData.m_upperLimit = br.readFloat();
    latestData.m_lowerLimit = br.readFloat();
    latestData.m_date = br.readDouble();
    return latestData;
}

void LatestServiceClient::onReceive(FCMessage *message) {
    FCClientService::onReceive(message);
    sendToListener(message);
}

void LatestServiceClient::onClientClose(int socketID, int localSID){
    FCClientService::onClientClose(socketID, localSID);
    m_connected = false;
}

